/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.bazi2.controllers;

import com.asseco.bazi2.models.Katalog;
import com.asseco.bazi2.models.KatalogSifrarnik;
import com.asseco.bazi2.models.Klient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.asseco.mio.codex.controllers.*;
import org.springframework.validation.Validator;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Bojan
 */
@Controller


@RequestMapping("/katalog*")
public class KatalogController extends BaseFormController {
    
     @RequestMapping(method = RequestMethod.GET)
    public ModelAndView onSubmit(Katalog katalog, BindingResult errors, HttpServletRequest request,
            HttpServletResponse response) throws SQLException {

      Configuration c = new Configuration().configure();
        SessionFactory sf = c.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();
        
        katalog = new Katalog();
        
        katalog.setId(0);

        Connection con = session.connection();  // obtain JDBC connection from Session object
        CallableStatement cs = con.prepareCall("{ call baza.SIFRARNIK_PACK.prebaruvanje_deca_roditel(?,?) }");
        
        cs.setInt(1, katalog.getId());
        
    
        
        List<KatalogSifrarnik> resultList = new ArrayList<KatalogSifrarnik>();


        cs.registerOutParameter(2, OracleTypes.CURSOR);
        cs.execute();
        ResultSet resultSet = (ResultSet) cs.getObject(2);
        if (resultSet != null) {
            while (resultSet.next()) {

                KatalogSifrarnik katalogSifrarnik = new KatalogSifrarnik();
                katalogSifrarnik.setIme(resultSet.getString(1));
                katalogSifrarnik.setId(resultSet.getInt(2));
             
                resultList.add(katalogSifrarnik);
            }
            resultSet.close();
        }

        session.getTransaction().commit();
        session.close();
        sf.close();
        
        return new ModelAndView().addObject("initialOptions", resultList);
        
      }
}
